package com.yida.gwj.common.controller;

import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import com.yida.common.core.domain.R;
import com.yida.common.core.utils.file.FileUtils;
import com.yida.common.core.utils.file.MimeTypeUtils;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.common.service.ISysFileService;
import com.yida.system.api.domain.SysFile;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileTypeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 文件请求处理
 *
 * @author yida
 */
@Api(value = "上传材料业务", tags = "上传材料业务")
// @RequestMapping("/sys/file")
// @RestController
public class SysFileController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileService sysFileService;

    @ApiOperation(value = "单文件上传")
    @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile", allowMultiple = true, paramType = "form")
    @RequiresPermissions("system:file:upload")
    @PostMapping("/upload")
    public R<SysFile> upload(@RequestPart MultipartFile file) {
        try {
            String fileType = FileTypeUtil.getType(file.getInputStream(), file.getOriginalFilename());
            // 检查允许上传的文件规则
            if (!ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, fileType)) {
                return R.fail("上传文件类型异常");
            }
            String url = sysFileService.uploadFile(file);
            SysFile sysFile = new SysFile();
            sysFile.setUrl(url);
            sysFile.setName(FileUtils.getName(url));
            // sysFile.setFileType(fileType);
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "多文件上传")
    @ApiImplicitParam(name = "files", value = "文件", dataType = "MultipartFile", allowMultiple = true, paramType = "form")
    @RequiresPermissions("system:file:upload")
    @PostMapping("/uploadBatch")
    public R<List<SysFile>> uploadBatch(@RequestPart MultipartFile[] files) {
        try {
            List<SysFile> sysFileList = ListUtil.toList();
            for (MultipartFile file : files) {
                String fileType = FileTypeUtil.getType(file.getInputStream(), file.getOriginalFilename());
                // 检查允许上传的文件规则
                if (!ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, fileType)) {
                    return R.fail("上传文件类型异常");
                }
                String url = sysFileService.uploadFile(file);
                SysFile sysFile = new SysFile();
                sysFile.setUrl(url);
                sysFile.setName(FileUtils.getName(url));
                // sysFile.setFileType(fileType);
                sysFileList.add(sysFile);
            }
            return R.ok(sysFileList);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

}
